1 | var canvas = document.getElementById("game");
|
2 |
|
3 | var manifest = {
|
4 | "images": {},
|
5 | "sounds": {},
|
6 | "fonts": [],
|
7 | "animations": {}
|
8 | };
|
9 | var animationTweaker = new Splat.Game(canvas, manifest);
|
10 | var animation;
|
11 | var loadCount = 0;
|
12 |
|
13 | animationTweaker.scenes.add("title", new Splat.Scene(canvas, function() {
|
14 | var a = animationTweaker.animations.get("animation" + loadCount);
|
15 | var x = (canvas.width / 2) - (a.width / 2) |0;
|
16 | var y = (canvas.height / 2) - (a.height / 2) |0;
|
17 | animation = new Splat.AnimatedEntity(x, y, a.width, a.height, a, 0, 0);
|
18 |
|
19 | controls = findControls();
|
20 | bindControls(animation);
|
21 |
|
22 | controls.width.value = animation.width;
|
23 | controls.height.value = animation.height;
|
24 | controls.offsetX.value = animation.spriteOffsetX;
|
25 | controls.offsetY.value = animation.spriteOffsetY;
|
26 | controls.running.checked = running;
|
27 | },
|
28 | function(elapsedMillis) {
|
29 | if (!running) {
|
30 | return;
|
31 | }
|
32 | animation.move(elapsedMillis);
|
33 | setControls(animation);
|
34 | }, function(context) {
|
35 | context.fillStyle = "#666666";
|
36 | context.fillRect(0, 0, canvas.width, canvas.height);
|
37 |
|
38 | context.strokeStyle = "#999999";
|
39 | context.beginPath();
|
40 | context.moveTo(canvas.width/2|0, 0);
|
41 | context.lineTo(canvas.width/2|0, canvas.height);
|
42 | context.stroke();
|
43 |
|
44 | context.beginPath();
|
45 | context.moveTo(0, canvas.height/2|0);
|
46 | context.lineTo(canvas.width, canvas.height/2|0);
|
47 | context.stroke();
|
48 |
|
49 | animation.draw(context);
|
50 |
|
51 | context.strokeStyle = "rgba(255, 0, 0, 0.5)"
|
52 | context.strokeRect(animation.x, animation.y, animation.width, animation.height);
|
53 | }));
|
54 |
|
55 | var running = true;
|
56 |
|
57 | function setControls(entity) {
|
58 | controls.frameCounter.textContent = "Frame " + (animation.sprite.frame + 1) + " of " + animation.sprite.frames.length;
|
59 | }
|
60 |
|
61 | function bindControls(entity) {
|
62 | controls.width.addEventListener("keyup", function() {
|
63 | entity.width = parseInt(controls.width.value);
|
64 | });
|
65 | controls.height.addEventListener("keyup", function() {
|
66 | entity.height = parseInt(controls.height.value);
|
67 | });
|
68 | controls.offsetX.addEventListener("keyup", function() {
|
69 | entity.spriteOffsetX = parseInt(controls.offsetX.value);
|
70 | });
|
71 | controls.offsetY.addEventListener("keyup", function() {
|
72 | entity.spriteOffsetY = parseInt(controls.offsetY.value);
|
73 | });
|
74 | controls.running.addEventListener("change", function() {
|
75 | running = controls.running.checked;
|
76 | });
|
77 | controls.step.addEventListener("click", function() {
|
78 | entity.sprite.step();
|
79 | setControls(entity);
|
80 | });
|
81 | controls.msPerFrame.addEventListener("keyup", function() {
|
82 | var animation = entity.sprite;
|
83 | var ms = parseInt(controls.msPerFrame.value);
|
84 | if (ms < 1) {
|
85 | ms = 1;
|
86 | }
|
87 |
|
88 | for (var i = 0; i < animation.frames.length; i++) {
|
89 | animation.frames[i].time = ms;
|
90 | }
|
91 | });
|
92 | }
|
93 |
|
94 | function loadFileIntoBuffer(file, callback) {
|
95 | var fr = new FileReader();
|
96 | fr.addEventListener("loadend", function() {
|
97 | var img = new Image();
|
98 | img.addEventListener("load", function() {
|
99 | var buffer = Splat.makeBuffer(img.width, img.height, function(context) {
|
100 | context.drawImage(img, 0, 0);
|
101 | });
|
102 | callback(buffer);
|
103 | });
|
104 | img.src = fr.result;
|
105 | });
|
106 | fr.readAsDataURL(file);
|
107 | }
|
108 |
|
109 | function getFrames(filename) {
|
110 | var matches = filename.match(/f(\d+)/);
|
111 | if (matches && matches.length > 1) {
|
112 | return parseInt(matches[1]);
|
113 | }
|
114 | return 1;
|
115 | }
|
116 |
|
117 | var controls;
|
118 | window.addEventListener("load", function() {
|
119 | controls = findControls();
|
120 |
|
121 | controls.file.addEventListener("change", function() {
|
122 | var file = controls.file.files[0];
|
123 | loadFileIntoBuffer(file, function(buffer) {
|
124 | animationTweaker.scenes.scenes["title"].stop();
|
125 | loadCount++;
|
126 |
|
127 | var name = "animation" + loadCount;
|
128 |
|
129 | animationTweaker.images.names.push(name);
|
130 | animationTweaker.images.images[name] = buffer;
|
131 |
|
132 | var frames = getFrames(file.name);
|
133 | controls.frames.innerHTML = frames;
|
134 | animationTweaker.animations.load(name, {
|
135 | "strip": name,
|
136 | "frames": frames,
|
137 | "msPerFrame": parseInt(controls.msPerFrame.value),
|
138 | });
|
139 | animationTweaker.scenes.switchTo("loading");
|
140 | });
|
141 | });
|
142 | });
|
143 |
|
144 | function findControls() {
|
145 | return {
|
146 | "width": document.getElementById("width"),
|
147 | "height": document.getElementById("height"),
|
148 | "offsetX": document.getElementById("offsetX"),
|
149 | "offsetY": document.getElementById("offsetY"),
|
150 | "running": document.getElementById("running"),
|
151 | "step": document.getElementById("step"),
|
152 | "frameCounter": document.getElementById("frameCounter"),
|
153 | "file": document.getElementById("file"),
|
154 | "frames": document.getElementById("frames"),
|
155 | "msPerFrame": document.getElementById("msPerFrame"),
|
156 | };
|
157 | }
|